import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.junit.Test;

public class CodeGenerator {

    @Test
    public void run() {

        // 1、创建代码生成器
        AutoGenerator autoGenerator = new AutoGenerator();

        // 2、全局配置
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setOutputDir(System.getProperty("user.dir") + "/src/main/java");
        globalConfig.setAuthor("CZY");
        globalConfig.setOpen(false); //生成后是否打开资源管理器
        globalConfig.setFileOverride(false); //重新生成时文件是否覆盖
        globalConfig.setServiceName("%sService");	//去掉Service接口的首字母I
        globalConfig.setIdType(IdType.ASSIGN_ID); //主键策略
        globalConfig.setDateType(DateType.ONLY_DATE);//定义生成的实体类中日期类型
        globalConfig.setSwagger2(true);//开启Swagger2模式

        autoGenerator.setGlobalConfig(globalConfig);

        // 3、数据源配置
        DataSourceConfig dataSourceConfig  = new DataSourceConfig();
        dataSourceConfig .setUrl("jdbc:mysql://127.0.0.1:3306/guli_ucenter?useSSL=false&useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&allowMultiQueries=true&serverTimezone=GMT%2B8");
        dataSourceConfig .setDriverName("com.mysql.cj.jdbc.Driver");
        dataSourceConfig .setUsername("erplocal");
        dataSourceConfig .setPassword("erplocal");
        dataSourceConfig .setDbType(DbType.MYSQL);
        autoGenerator.setDataSource(dataSourceConfig );

        // 4、包配置
        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setParent("com.isczy");
        packageConfig.setModuleName("userservice"); //模块名
        packageConfig.setController("controller");
        packageConfig.setEntity("entity");
        packageConfig.setService("service");
        packageConfig.setMapper("mapper");
        autoGenerator.setPackageInfo(packageConfig);

        // 5、策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setInclude("ucenter_member");//对应那张表生成，可以逗号隔开对应多张表
        strategy.setNaming(NamingStrategy.underline_to_camel);//数据库表映射到实体的命名策略
        strategy.setTablePrefix(packageConfig.getModuleName() + "_"); //生成实体时去掉表前缀
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);//数据库表字段映射到实体的命名策略
        strategy.setEntityLombokModel(true); // lombok 模型 @Accessors(chain = true) setter链式操作
        strategy.setRestControllerStyle(true); //restful api风格控制器
        strategy.setControllerMappingHyphenStyle(true); //url中驼峰转连字符
        autoGenerator.setStrategy(strategy);

        // 6、执行
        autoGenerator.execute();
    }
}
